#include "PlayersStatsMenu.h"
#include "PlayersStatsShowMenu.h"
#include "StringUtilities.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "LeagueManager.h"
#include "PeopleManager.h"
#include "League.h"
#include "Team.h"

#include "StlStringAndStreams.h"

#include <cassert>

PlayersStatsMenu::PlayersStatsMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}

void PlayersStatsMenu::Draw() const
{
	using STD::cout;
	using STD::endl;

	cout << Print::Title("PLAYERS STATISTICS:");

	cout << "1) All Time Players" << endl;
	cout << "2) All Retired HOF Players" << endl;
	cout << "3) All Active Players" << endl;
	cout << "4) All Free Players" << endl;
	cout << "5) All Leagues Players" << endl;
	size_t counter = NUM_FIXED_OPTIONS;
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);
	for (LeagueVecCit it = leagues.begin(); it != leagues.end(); ++it)
		cout << ++counter << ") " << (*it)->GetName() << endl;
	cout << endl << endl;

	STL::string options = "1-" + Utilities::IntToStlString(counter) + ") Select Option.\n\n0) Return to previous menu'.";
	cout << Print::Options(options) << "Selection: ";
}

bool PlayersStatsMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;
	const size_t res = Utilities::StringToInt(i_input);

	if (res == 0)
	{
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	}
	else if(res > 0 && res <= NUM_FIXED_OPTIONS + GetSP()->GetLeagueMgr().GetLeagues(true).size())
	{
		SetPeopleVector(res);
		GetMM()->SetMenu(GetMM()->GetPlayersStatsShowMenu());
	}
	else
	{
		goodInput = false;
	}

	return goodInput;
}

void PlayersStatsMenu::SetPeopleVector(const int i_selection)
{
	const LeagueManager& LM = GetSP()->GetLeagueMgr();
	PeopleManager& TM = GetSP()->GetPeopleMgr();

	PlayersStatsShowMenu* nextMenu = static_cast<PlayersStatsShowMenu*>(GetMM()->GetPlayersStatsShowMenu());
	PeopleVec& thePlayers = nextMenu->m_players;
	thePlayers.clear();

	switch(i_selection)
	{
	case 1:
		thePlayers = TM.GetFreeVec(true);
		Utilities::Append(thePlayers, TM.GetBusyVec(true));
		Utilities::Append(thePlayers, TM.GetFreeVec(false));
		Utilities::Append(thePlayers, TM.GetBusyVec(false));
		Utilities::Append(thePlayers, TM.GetHofVec());
		break;

	case 2:
		thePlayers = TM.GetHofVec();
		Utilities::Append(thePlayers, TM.GetFreeVec(false));
		Utilities::Append(thePlayers, TM.GetBusyVec(false));
		break;

	case 3:
		thePlayers = TM.GetFreeVec(true);
		Utilities::Append(thePlayers, TM.GetBusyVec(true));
		break;

	case 4:
		thePlayers = TM.GetFreeVec(true);
		break;

	case 5:
		thePlayers = TM.GetBusyVec(true);
		break;

	default:
		{
			const size_t leagueNum = i_selection - (NUM_FIXED_OPTIONS + 1);
			const LeagueVec& theLeagues = LM.GetLeagues(true);
			assert(leagueNum < theLeagues.size());
			const League* league = theLeagues[leagueNum];

			thePlayers.reserve(league->GetSettings().numOfTeams * Team::GetParams().MAX_TEAM_PLAYERS);
			for(TeamVecCit tit = league->GetTeams().begin(); tit != league->GetTeams().end(); ++tit)
				for(PeopleVecCit pit = (*tit)->GetPlayersList().begin(); pit != (*tit)->GetPlayersList().end(); ++pit)
					thePlayers.push_back((*pit));
		}
		break;
	}
}


